# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
# RUN:    -experimental-debug-variable-locations \
# RUN:    -start-before=phi-node-elimination \
# RUN:    -stop-after=simple-register-coalescing \
# RUN:    | FileCheck %s --check-prefix=DOESCOALESCE
# RUN: llc %s -o - -mtriple=x86_64-unknown-unknown \
# RUN:    -experimental-debug-variable-locations \
# RUN:    -start-before=phi-node-elimination \
# RUN:    -stop-after=virtregrewriter \
# RUN:    | FileCheck %s --check-prefix=CHECK
#
# This is a sibling-test for phi-coalescing.mir, using similar code but where
# registers are coalesced across subregisters. Specifically, vreg %2 below is
# a 16-bit register, but will be coalesced into the lower 16 bits of a 32 bit
# vreg during coalescing. We should survive this transformation, and emit a
# DBG_PHI for $bx where the PHI was.
--- |
  ; ModuleID = 'promoted.ll'
  source_filename = "test.c"
  target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
  target triple = "x86_64-unknown-linux-gnu"
  
  ; Function Attrs: noinline nounwind uwtable
  define dso_local i32 @foo(i16 signext %bar, i16 signext %baz) !dbg !7 {
  entry:
    call void @llvm.dbg.value(metadata i16 %bar, metadata !12, metadata !DIExpression()), !dbg !13
    call void @llvm.dbg.value(metadata i16 %baz, metadata !14, metadata !DIExpression()), !dbg !13
    call void @ext(i16 signext %bar), !dbg !13
    %conv = sext i16 %bar to i32, !dbg !13
    %add = add nsw i32 %conv, 12, !dbg !13
    %conv1 = trunc i32 %add to i16, !dbg !13
    call void @llvm.dbg.value(metadata i16 %conv1, metadata !12, metadata !DIExpression()), !dbg !13
    call void @ext(i16 signext %conv1), !dbg !13
    %call = call signext i16 @getlong(), !dbg !13
    %tobool = icmp ne i16 %call, 0, !dbg !13
    br i1 %tobool, label %if.then, label %if.end, !dbg !13
  
  if.then:                                          ; preds = %entry
    %conv2 = sext i16 %conv1 to i32, !dbg !13
    %add3 = add nsw i32 %conv2, 1, !dbg !13
    %conv4 = trunc i32 %add3 to i16, !dbg !13
    call void @llvm.dbg.value(metadata i16 %conv4, metadata !12, metadata !DIExpression()), !dbg !13
    br label %if.end, !dbg !13
  
  if.end:                                           ; preds = %if.then, %entry
    %bar.addr.0 = phi i16 [ %conv4, %if.then ], [ %conv1, %entry ], !dbg !13
    call void @llvm.dbg.value(metadata i16 %bar.addr.0, metadata !12, metadata !DIExpression()), !dbg !13
    %conv5 = sext i16 %baz to i32, !dbg !13
    %conv6 = sext i16 %bar.addr.0 to i32, !dbg !13
    %add7 = add nsw i32 %conv6, %conv5, !dbg !13
    %conv8 = trunc i32 %add7 to i16, !dbg !13
    call void @llvm.dbg.value(metadata i16 %conv8, metadata !12, metadata !DIExpression()), !dbg !13
    call void @ext(i16 signext %conv8), !dbg !13
    %conv9 = sext i16 %conv8 to i32, !dbg !13
    ret i32 %conv9, !dbg !13
  }
  
  ; Function Attrs: nounwind readnone speculatable willreturn
  declare void @llvm.dbg.declare(metadata, metadata, metadata)
  
  declare dso_local void @ext(i16 signext)
  
  declare dso_local signext i16 @getlong()
  
  ; Function Attrs: nounwind readnone speculatable willreturn
  declare void @llvm.dbg.value(metadata, metadata, metadata)
  
  !llvm.dbg.cu = !{!0}
  !llvm.module.flags = !{!3, !4, !5}
  !llvm.ident = !{!6}
  
  !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 12.0.0 (git@github.com:llvm/llvm-project 79a35789efdf2378f97642ae4a5f3099b9087a11)", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
  !1 = !DIFile(filename: "test.c", directory: "/fast/fs/llvm3/llvm/test/DebugInfo/MIR/InstrRef")
  !2 = !{}
  !3 = !{i32 7, !"Dwarf Version", i32 4}
  !4 = !{i32 2, !"Debug Info Version", i32 3}
  !5 = !{i32 1, !"wchar_size", i32 4}
  !6 = !{!"clang version 12.0.0 (git@github.com:llvm/llvm-project 79a35789efdf2378f97642ae4a5f3099b9087a11)"}
  !7 = distinct !DISubprogram(name: "foo", scope: !1, file: !1, line: 3, type: !8, scopeLine: 3, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
  !8 = !DISubroutineType(types: !9)
  !9 = !{!10, !11, !11}
  !10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
  !11 = !DIBasicType(name: "short", size: 16, encoding: DW_ATE_signed)
  !12 = !DILocalVariable(name: "bar", arg: 1, scope: !7, file: !1, line: 3, type: !11)
  !13 = !DILocation(line: 0, scope: !7)
  !14 = !DILocalVariable(name: "baz", arg: 2, scope: !7, file: !1, line: 3, type: !11)
  !15 = distinct !DILexicalBlock(scope: !7, file: !1, line: 8, column: 7)

...
---
name:            foo
alignment:       16
tracksRegLiveness: true
liveins:
  - { reg: '$edi', virtual-reg: '%3' }
  - { reg: '$esi', virtual-reg: '%4' }
frameInfo:
  maxAlignment:    1
  hasCalls:        true
machineFunctionInfo: {}
body:             |
  bb.0.entry:
    liveins: $edi, $esi
  
    %4:gr32 = COPY $esi
    %3:gr32 = COPY $edi
    %6:gr16 = COPY %4.sub_16bit
    %5:gr16 = COPY %3.sub_16bit
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %15:gr32 = MOVSX32rr16 %5, debug-location !13
    $edi = COPY %15, debug-location !13
    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $edi, debug-location !13
    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %14:gr32 = MOVSX32rr16 %5, debug-location !13
    %13:gr32 = ADD32ri8 killed %14, 12, implicit-def $eflags, debug-location !13
    %11:gr16 = COPY killed %13.sub_16bit, debug-location !13
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %9:gr32 = MOVSX32rr16 %11, debug-location !13
    $edi = COPY %9, debug-location !13
    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $edi, debug-location !13
    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    CALL64pcrel32 @getlong, csr_64, implicit $rsp, implicit $ssp, implicit-def $ax, debug-location !13
    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %8:gr16 = COPY $ax, debug-location !13
    CMP16ri8 %8, 0, implicit-def $eflags, debug-location !13
    JCC_1 %bb.2, 4, implicit $eflags, debug-location !13
  
  ; DOESCOALESCE-LABEL: bb.1.if.then:
  ; CHECK-LABEL:        bb.1.if.then:
  bb.1.if.then:
    %20:gr32 = MOVSX32rr16 %11, debug-location !13
    %19:gr32 = ADD32ri8 killed %20, 1, implicit-def $eflags, debug-location !13
    %17:gr16 = COPY killed %19.sub_16bit, debug-location !13

  ; Verify that vreg 17 is coalesced into a gr32, and not copied any further.
  ; DOESCOALESCE:       %{{[0-9]+}}:gr32 = ADD32ri8
  ; DOESCOALESCE-NOT:   COPY
  ; Verify those registers land in $bx
  ; CHECK:              renamable $ebp = ADD32ri8

  ; DOESCOALESCE-LABEL: bb.2.if.end:
  ; CHECK-LABEL:        bb.2.if.end:
  bb.2.if.end:
    %2:gr16 = PHI %11, %bb.0, %17, %bb.1, debug-instr-number 1, debug-location !13
  ; CHECK:              DBG_PHI $bp, 1
    DBG_INSTR_REF 1, 0, !12, !DIExpression(), debug-location !13
    %31:gr32 = MOVSX32rr16 %6, debug-location !13
    %30:gr32 = MOVSX32rr16 killed %2, debug-location !13
    %29:gr32 = ADD32rr killed %30, killed %31, implicit-def $eflags, debug-location !13
    %26:gr16 = COPY killed %29.sub_16bit, debug-location !13
    ADJCALLSTACKDOWN64 0, 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %24:gr32 = MOVSX32rr16 %26, debug-location !13
    $edi = COPY %24, debug-location !13
    CALL64pcrel32 @ext, csr_64, implicit $rsp, implicit $ssp, implicit $edi, debug-location !13
    ADJCALLSTACKUP64 0, 0, implicit-def $rsp, implicit-def $eflags, implicit-def $ssp, implicit $rsp, implicit $ssp, debug-location !13
    %23:gr32 = MOVSX32rr16 %26, debug-location !13
    $eax = COPY %23, debug-location !13
    RETQ implicit $eax, debug-location !13

...
